home *** CD-ROM | disk | FTP | other *** search
/ Gamers Delight 2 / Gamers Delight 2.iso / Aminet / game / misc / TownMaze.lha / TownMaze / src.lzh / makegates.c < prev    next >
C/C++ Source or Header  |  1991-08-04  |  3KB  |  105 lines

  1. /*
  2. ** makegates.c  Copyright 1991 Kent Paul Dolan,
  3. **              Mountain View, CA, USA 94039-0755
  4. **
  5. ** Written to satisfy an inquiry on USENet's rec.games.programmer newsgroup.
  6. ** May be freely used or modified in any non-commercial work.  Copyrighted
  7. ** only to prevent patenting by someone else.
  8. */
  9.  
  10. #include <stdio.h>
  11. #include "townmaze.h"
  12. #include "townproto.h"
  13.  
  14. #ifdef __STDC__
  15. void makegates()
  16. #else
  17. int makegates()
  18. #endif
  19. {
  20.  
  21.   int gatewalls;
  22.   int chosenwall;
  23.   int chosencell;
  24.   int stdir;
  25.   int tries;
  26.   int i;
  27.  
  28. /*
  29. ** Pepper gates around the outer wall; avoid gates at corners for
  30. ** esthetic reasons.
  31. */
  32.  
  33. #if PROGRESS
  34.   fprintf(stderr,"Gates ");
  35. #endif
  36.  
  37.   gatewalls = 2 * (mazeheight/2 + mazewidth/2);
  38.  
  39.   for (i = 0; i < mazegates; i++)
  40.   {
  41.  
  42. /*  fprintf(stderr,"Gate %d\n",i); */
  43.  
  44. /*
  45. ** Protect against infinite looping.
  46. */
  47.     tries = 0;
  48. /*
  49. ** Keep looking until a candidate cell is found for this ith gate.
  50. */
  51.     do
  52.     {
  53.       /* not perfectly fair, but good enough for moderate sized mazes. */
  54.       chosenwall = RANDOM()%gatewalls;
  55.                                        
  56. /*    fprintf(stderr,"  chosenwall %d\n",chosenwall); */
  57.       if (chosenwall < (mazewidth/2))
  58.       {
  59.         chosencell = chosenwall;
  60.         stdir = 2;
  61. /*      fprintf(stderr,"    top chosencell %d\n",chosencell); */
  62.       }
  63.       else
  64.         if (chosenwall < ((mazewidth/2) + (mazeheight/2)))
  65.         {
  66.           chosencell = ((chosenwall - (mazewidth/2) + 1) 
  67.                                       * (mazewidth/2) - 1);
  68.           stdir = 3;
  69. /*        fprintf(stderr,"      right chosencell %d\n",chosencell); */
  70.         }
  71.         else
  72.           if (chosenwall < (mazewidth - 1 + (mazeheight/2)))
  73.           {
  74.             chosencell = (listsize - chosenwall + (mazewidth/2) 
  75.                           + (mazeheight/2) - 1);
  76.             stdir = 0;
  77. /*          fprintf(stderr,"        bottom chosencell %d\n",chosencell); */
  78.           }
  79.           else
  80.           {
  81.             chosencell = (mazewidth - 1 + mazeheight - 1 - chosenwall - 1)
  82.                          * (mazewidth/2);
  83.             stdir = 1;
  84. /*          fprintf(stderr,"          left chosencell %d\n",chosencell); */
  85.  
  86.           }
  87.       tries++;
  88.     } while (   (tries <= MAXTRIES)
  89.              && (   (statlist[chosencell].status != ISOLATED)
  90.                  || (   nhbrexists(chosencell,0)
  91.                      && (statlist[nhbris(chosencell,0)].status != ISOLATED) )
  92.                  || (   nhbrexists(chosencell,1)
  93.                      && (statlist[nhbris(chosencell,1)].status != ISOLATED) )
  94.                  || (   nhbrexists(chosencell,2)
  95.                      && (statlist[nhbris(chosencell,2)].status != ISOLATED) )
  96.                  || (   nhbrexists(chosencell,3)
  97.                      && (statlist[nhbris(chosencell,3)].status != ISOLATED) )
  98.                 )
  99.             );
  100.  
  101.     if (tries <= MAXTRIES) makestreet(chosencell,streetnumct++,stdir);
  102.   }
  103.   return;
  104. }
  105.